package gui;


import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import client.Client;
import javax.swing.JRadioButton;

public class Order3 extends JPanel {
	
	final public static int DEFAULT_PORT = 5555;	
    private String host = MainPanel.ip; 
	private int temp;
	private int sub=0;
	String[] newbalance;
	private JLabel jLabelpaymeth = null;
	private JButton jButtonHelp = null;
	JButton jButtonExit = null;
	JButton jButtonBack = null;
	JButton jButtoncash = null;
	JButton jButtoncreditcard = null;
	JButton jButtonsubscription = null;
	private JButton jButtoninsyscar = null;
	private JLabel jLabelcredit = null;
	private JButton jButtonOrder = null;
	private JLabel jLabelSubNum = null;
	private JLabel jLabelAmountAccount = null;
	private JLabel jLabelAmountAccountAfter = null;
	private JLabel jLabelErrorMassage1 = null;
	private JTextField jTextFieldSubNum = null;
	private JTextField jTextFieldAccountBefore = null;
	private JTextField jTextFieldAmountAfter = null;
	private JLabel jLabelCreditCardNum = null;
	private JTextField jTextFieldCreditNum = null;
	private JLabel jLabelExpiry = null;
	private JTextField jTextFieldExpiry = null;
	private JLabel jLabelExample = null;
	private JLabel jLabelErrorMassage2 = null;
	private JLabel jLabelErrorMassage3 = null;
	private JButton jButtonCheckValid = null;
	private JLabel jLabelValid = null;
	/**
	 * This is the default constructor
	 */
	public Order3() {
		super();
		initialize();
	}
	
	/**
	 * This method initializes order3
	 */
	private void initialize() {		
		jLabelValid = new JLabel();
		jLabelValid.setBounds(new Rectangle(410, 545, 101, 38));
		jLabelValid.setText("CARD IS VALID");
		jLabelValid.setVisible(false);
		jLabelErrorMassage3 = new JLabel();
		jLabelErrorMassage3.setBounds(new Rectangle(24, 571, 533, 56));
		jLabelErrorMassage3.setText("YOUR CREDIT-CARD IS NOT REGISTERD IN THE SYSTEM. PLEASE CONTACT SYSTEM MANAGER");
		jLabelErrorMassage3.setVisible(false);
		jLabelErrorMassage2 = new JLabel();
		jLabelErrorMassage2.setBounds(new Rectangle(62, 571, 465, 58));
		jLabelErrorMassage2.setText("YOU ARE NOT REGISTERED AS SUBSCRIBER. PLEASE CONTACT SYSTEM MANAGE");
		jLabelErrorMassage2.setVisible(false);
		jLabelExample = new JLabel();
		jLabelExample.setBounds(new Rectangle(64, 571, 387, 33));
		jLabelExample.setText("FOR EXAMPLE: 1401 WHERE 14 IS THE YEAR AND 01 IS THE MONTH");
		jLabelExample.setVisible(false);
		jLabelExpiry = new JLabel();
		jLabelExpiry.setBounds(new Rectangle(115, 526, 88, 31));
		jLabelExpiry.setText("EXPIRY DATE:");
		jLabelExpiry.setVisible(false);
		jLabelCreditCardNum = new JLabel();
		jLabelCreditCardNum.setBounds(new Rectangle(114, 482, 143, 32));
		jLabelCreditCardNum.setText("CREDIT-CARD NUMBER:");
		jLabelCreditCardNum.setVisible(false);
		jLabelErrorMassage1 = new JLabel();
		jLabelErrorMassage1.setBounds(new Rectangle(61, 571, 466, 59));
		jLabelErrorMassage1.setText("YOU DONT HAVE ANOUGH FOUNDS IN THE ACCOUNT TO MAKE THIS PURCHASE");
		jLabelErrorMassage1.setVisible(false);
		jLabelAmountAccountAfter = new JLabel();
		jLabelAmountAccountAfter.setBounds(new Rectangle(30, 570, 272, 36));
		jLabelAmountAccountAfter.setText("AMOUNT IN THE ACCOUNT AFTER PURCHASE:");
		jLabelAmountAccountAfter.setVisible(false);
		jLabelAmountAccount = new JLabel();
		jLabelAmountAccount.setBounds(new Rectangle(30, 525, 160, 33));
		jLabelAmountAccount.setText("AMOUNT IN THE ACCOUNT:");
		jLabelAmountAccount.setVisible(false);
		jLabelSubNum = new JLabel();
		jLabelSubNum.setBounds(new Rectangle(31, 481, 160, 32));
		jLabelSubNum.setText("SUBSCRIPTION NUMBER:");
		jLabelSubNum.setVisible(false);
		jLabelcredit = new JLabel();
		jLabelcredit.setBounds(new Rectangle(242, 249, 96, 28));
		jLabelcredit.setText("CREDIT CARD:");
		jLabelcredit.setVisible(false);
		ImageIcon imagePay = new ImageIcon("CLICK2PAY_logo_640.jpg");
		jLabelpaymeth = new JLabel();
		jLabelpaymeth.setIcon(imagePay);
		jLabelpaymeth.setBounds(new Rectangle(35, 22, 521, 90));
	//	jLabelpaymeth.setText("how would you like to pay?");
		this.setSize(600, 800);
		this.setLayout(null);
		this.setBackground(Color.cyan);
		this.add(jLabelpaymeth, null);
		this.add(getJButtonHelp(), null);
		this.add(getJButtonExit(), null);
		this.add(getJButtonBack(), null);
		this.add(getJButtoncash(), null);
		this.add(getJButtoncreditcard(), null);
		this.add(getJButtonsubscription(), null);
		this.add(getJButtoninsyscar(), null);
		this.add(jLabelcredit, null);
		this.add(getJButtonOrder(), null);
		this.add(jLabelSubNum, null);
		this.add(jLabelAmountAccount, null);
		this.add(jLabelAmountAccountAfter, null);
		this.add(jLabelErrorMassage1, null);
		this.add(getJTextFieldSubNum(), null);
		this.add(getJTextFieldAccountBefore(), null);
		this.add(getJTextFieldAmountAfter(), null);
		this.add(jLabelCreditCardNum, null);
		this.add(getJTextFieldCreditNum(), null);
		this.add(jLabelExpiry, null);
		this.add(getJTextFieldExpiry(), null);
		this.add(jLabelExample, null);
		this.add(jLabelErrorMassage2, null);
		this.add(jLabelErrorMassage3, null);
		this.add(getJButtonCheckValid(), null);
		this.add(jLabelValid, null);
      
	}
	/**
	 * This method initializes jButtonHelp and opens the help screen
	 * @return
	 */
	private JButton getJButtonHelp() {
		if (jButtonHelp == null) {
			jButtonHelp = new JButton();
			jButtonHelp.setBounds(new Rectangle(14, 737, 106, 45));
			jButtonHelp.setText("HELP");
			jButtonHelp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					HelpFrame HFrame = new HelpFrame();

					HFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
					HFrame.setSize( 600,720 ); // set frame size
					HFrame.setResizable(false);
					HFrame.setVisible( true ); // display frame
				}
			});
		}
		return jButtonHelp;
	}

	/**
	 * This method initializes jButtonExit and opens the exit screen	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonExit() {
		if (jButtonExit == null) {
			jButtonExit = new JButton();
			jButtonExit.setBounds(new Rectangle(479, 738, 91, 44));
			jButtonExit.setText("EXIT");

		}
		return jButtonExit;
	}

	/**
	 * This method initializes jButtonBack and opens the Order1 screen
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonBack() {
		if (jButtonBack == null) {
			jButtonBack = new JButton();
			jButtonBack.setBounds(new Rectangle(297, 738, 87, 44));
			jButtonBack.setText("BACK");
		}
		return jButtonBack;
	}


	/**
	 * This method initializes jButtoncash	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtoncash() {
		if (jButtoncash == null) {
			jButtoncash = new JButton();
			jButtoncash.setBounds(new Rectangle(70, 157, 179, 55));
			jButtoncash.setText("CASH");
			jButtoncash.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jLabelErrorMassage1.setVisible(false);
					jLabelErrorMassage2.setVisible(false);
					jLabelErrorMassage3.setVisible(false);
					jLabelAmountAccountAfter.setVisible(false);
					jLabelAmountAccount.setVisible(false);
					jLabelSubNum.setVisible(false);						
					jTextFieldSubNum.setVisible(false);						
					jTextFieldAccountBefore.setVisible(false);
					jTextFieldAmountAfter.setVisible(false);
					jLabelExpiry.setVisible(false);
					jLabelCreditCardNum.setVisible(false);
					jTextFieldCreditNum.setVisible(false);
					jTextFieldExpiry.setVisible(false);	
					jLabelExample.setVisible(false);
					jLabelExpiry.setVisible(false);
					jLabelCreditCardNum.setVisible(false);
					jTextFieldCreditNum.setVisible(false);
					jTextFieldExpiry.setVisible(false);	
					jButtonCheckValid.setVisible(false);
					
					ErrorFrame EFrame = new ErrorFrame();
					EFrame.jLabelMassage.setText("PLEASE PRESS ORDER AND STEP OVER TO THE COUNTER");
					EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
					EFrame.setSize( 380,217 ); // set frame size
					EFrame.setResizable(true);
					EFrame.setVisible( true ); 
					
					if (!(Order2.statecount%2==0))
						temp=temp-Order1.calc-30;
							else
								temp=temp-Order1.calc;
					
					String[] payment = new String[2];
					payment[0]="cash";
					payment[1]=Integer.toString(temp);
					Order1.OrderDeatails.add(payment);
				}
			});
			
		}
		return jButtoncash;
	}


	/**
	 * This method initializes jButtoncreditcard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtoncreditcard() {
		if (jButtoncreditcard == null) {
			jButtoncreditcard = new JButton();
			jButtoncreditcard.setBounds(new Rectangle(207, 301, 179, 55));
			jButtoncreditcard.setText("NEW CREDIT CARD");
			jButtoncreditcard.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jLabelErrorMassage1.setVisible(false);
					jLabelErrorMassage2.setVisible(false);
					jLabelErrorMassage3.setVisible(false);
					jLabelAmountAccountAfter.setVisible(false);
					jLabelAmountAccount.setVisible(false);
					jLabelSubNum.setVisible(false);						
					jTextFieldSubNum.setVisible(false);						
					jTextFieldAccountBefore.setVisible(false);
					jTextFieldAmountAfter.setVisible(false);
					jLabelExpiry.setVisible(false);
					jLabelCreditCardNum.setVisible(false);
					jTextFieldCreditNum.setVisible(false);
					jTextFieldExpiry.setVisible(false);	
					jLabelExample.setVisible(false);
					jLabelExpiry.setVisible(false);
					jLabelCreditCardNum.setVisible(false);
					jTextFieldCreditNum.setVisible(false);
					jTextFieldExpiry.setVisible(false);	
					jButtonCheckValid.setVisible(false);
					
					jTextFieldCreditNum.setText("");
					jTextFieldExpiry.setText("");
					jLabelExample.setVisible(true);
					jLabelExpiry.setVisible(true);
					jLabelCreditCardNum.setVisible(true);
					jTextFieldCreditNum.setVisible(true);
					jTextFieldExpiry.setVisible(true);	
					jButtonCheckValid.setVisible(true);
					
					if (!(Order2.statecount%2==0))
						temp=temp-Order1.calc-30;
							else
								temp=temp-Order1.calc;
					
					String[] payment = new String[2];
					payment[0]="new_credit_card";
					payment[1]=Integer.toString(temp);
					Order1.OrderDeatails.add(payment);
				}
			});
			
		}
		return jButtoncreditcard;
	}


	/**
	 * This method initializes jButtonsubscription	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonsubscription() {
		if (jButtonsubscription == null) {
			jButtonsubscription = new JButton();
			jButtonsubscription.setBounds(new Rectangle(350, 157, 179, 55));
			jButtonsubscription.setText("SUBSCRIPTION");
			jButtonsubscription.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					sub=1;
					jLabelErrorMassage1.setVisible(false);
					jLabelErrorMassage2.setVisible(false);
					jLabelErrorMassage3.setVisible(false);
					jLabelExample.setVisible(false);
					jLabelExpiry.setVisible(false);
					jLabelCreditCardNum.setVisible(false);
					jTextFieldCreditNum.setVisible(false);
					jTextFieldExpiry.setVisible(false);	
					jButtonCheckValid.setVisible(false);
					
					Client CheckSub = new Client(host,DEFAULT_PORT);
					String[] checksub = new String[2];
					checksub[0]="check-subscription";
					checksub[1]=MainPanel.Userinfo[3];					
					
										
					
					CheckSub.accept(checksub);
					Object p=CheckSub.getMsg();
					Vector<Object> state=(Vector)p;

					if ((Integer)state.firstElement()==0)
					{
						jLabelErrorMassage2.setVisible(true);						
					}
					else
					{						
						String subnum = Integer.toString((Integer)state.get(1));
						jTextFieldSubNum.setText(subnum);
						String money = Integer.toString((Integer)state.get(2));
						jTextFieldAccountBefore.setText(money);
						temp=(Integer)state.get(2);
						if (Order1.calc>temp)
							jLabelErrorMassage1.setVisible(true);
						else
						{
							jLabelAmountAccountAfter.setVisible(true);
							jLabelAmountAccount.setVisible(true);
							jLabelSubNum.setVisible(true);						
							jTextFieldSubNum.setVisible(true);						
							jTextFieldAccountBefore.setVisible(true);
							jTextFieldAmountAfter.setVisible(true);
						
							if (!(Order2.statecount%2==0))
						temp=temp-Order1.calc-30;
							else
								temp=temp-Order1.calc;	
							
							
						newbalance = new String[3];
						newbalance[0]="updatebalance";						
						newbalance[1]=Integer.toString(temp);
						newbalance[2]=MainPanel.Userinfo[3];
						jTextFieldAmountAfter.setText(newbalance[1]);
					//	CheckSub.accept(newbalance);						
						String[] payment = new String[2];
						payment[0]="subsription";
						payment[1]=Integer.toString(temp);
						Order1.OrderDeatails.add(payment);
						}
					}
						
					
				}
			});
			
		}
		return jButtonsubscription;
	}


	/**
	 * This method initializes jButtoninsyscar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtoninsyscar() {
		if (jButtoninsyscar == null) {
			jButtoninsyscar = new JButton();
			jButtoninsyscar.setBounds(new Rectangle(112, 400, 381, 55));
			jButtoninsyscar.setText("I WANT TO PAY WITH THE CARD I PAYED ON MY LAST BUY");
			jButtoninsyscar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jLabelErrorMassage1.setVisible(false);
					jLabelErrorMassage2.setVisible(false);
					jLabelErrorMassage3.setVisible(false);
					jLabelAmountAccountAfter.setVisible(false);
					jLabelAmountAccount.setVisible(false);
					jLabelSubNum.setVisible(false);						
					jTextFieldSubNum.setVisible(false);						
					jTextFieldAccountBefore.setVisible(false);
					jTextFieldAmountAfter.setVisible(false);
					jLabelExpiry.setVisible(false);
					jLabelCreditCardNum.setVisible(false);
					jTextFieldCreditNum.setVisible(false);
					jTextFieldExpiry.setVisible(false);	
					jLabelExample.setVisible(false);
					jLabelExpiry.setVisible(false);
					jLabelCreditCardNum.setVisible(false);
					jTextFieldCreditNum.setVisible(false);
					jTextFieldExpiry.setVisible(false);	
					jButtonCheckValid.setVisible(false);
					
					
					Client LastTime = new Client(host,DEFAULT_PORT);
					String[] lasttime = new String[2];
					lasttime[0]="last_time";
					lasttime[1]=MainPanel.Userinfo[3];																								
					LastTime.accept(lasttime);
					Object p=LastTime.getMsg();
					Vector<Object> state=(Vector)p;

					if ((Integer)state.firstElement()==0)
					{
						jLabelErrorMassage3.setVisible(true);						
					}
					else
					{
						jLabelExpiry.setVisible(true);
						jLabelCreditCardNum.setVisible(true);
						jTextFieldCreditNum.setVisible(true);
						jTextFieldExpiry.setVisible(true);							
						jTextFieldCreditNum.setText(Integer.toString((Integer)state.elementAt(1)));				
						jTextFieldExpiry.setText(Integer.toString((Integer)state.elementAt(2)));
						if (!(Order2.statecount%2==0))
							temp=temp-Order1.calc-30;
								else
									temp=temp-Order1.calc;
						
						String[] payment = new String[2];
						payment[0]="old_credit_card";
						payment[1]=Integer.toString(temp);
						Order1.OrderDeatails.add(payment);
					}
				}
			});
		}
		return jButtoninsyscar;
	}


	/**
	 * This method initializes jButtonOrder	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOrder() {
		if (jButtonOrder == null) {
			jButtonOrder = new JButton();
			jButtonOrder.setBounds(new Rectangle(390, 738, 84, 44));
			jButtonOrder.setText("ORDER");
			jButtonOrder.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Client Order = new Client(host,DEFAULT_PORT);
					if (sub==1)
						Order.accept(newbalance);
						Order.accept(Order1.OrderDeatails);
						ErrorFrame EFrame = new ErrorFrame();
						EFrame.jLabelMassage.setText("YOUR ORDER HAS BEEN MADE, PLEASE EXIT THIS WINDOW");
						EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
						EFrame.setSize( 480,217 ); // set frame size
						EFrame.setResizable(true);
						EFrame.setVisible( true );
				}
			});
		}
		return jButtonOrder;
	}


	/**
	 * This method initializes jTextFieldSunNum	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldSubNum() {
		if (jTextFieldSubNum == null) {
			jTextFieldSubNum = new JTextField();
			jTextFieldSubNum.setBounds(new Rectangle(195, 481, 62, 32));
			jTextFieldSubNum.setVisible(false);
		}
		return jTextFieldSubNum;
	}


	/**
	 * This method initializes jTextFieldAccountBefore	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldAccountBefore() {
		if (jTextFieldAccountBefore == null) {
			jTextFieldAccountBefore = new JTextField();
			jTextFieldAccountBefore.setBounds(new Rectangle(195, 525, 62, 31));
			jTextFieldAccountBefore.setVisible(false);
		}
		return jTextFieldAccountBefore;
	}


	/**
	 * This method initializes jTextFieldAmountAfter	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldAmountAfter() {
		if (jTextFieldAmountAfter == null) {
			jTextFieldAmountAfter = new JTextField();
			jTextFieldAmountAfter.setBounds(new Rectangle(306, 571, 69, 35));
			jTextFieldAmountAfter.setVisible(false);
		}
		return jTextFieldAmountAfter;
	}


	/**
	 * This method initializes jTextFieldCreditNum	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCreditNum() {
		if (jTextFieldCreditNum == null) {
			jTextFieldCreditNum = new JTextField();
			jTextFieldCreditNum.setBounds(new Rectangle(267, 480, 108, 36));
			jTextFieldCreditNum.setVisible(false);
		}
		return jTextFieldCreditNum;
	}


	/**
	 * This method initializes jTextFieldExpiry	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldExpiry() {
		if (jTextFieldExpiry == null) {
			jTextFieldExpiry = new JTextField();
			jTextFieldExpiry.setBounds(new Rectangle(210, 525, 76, 34));
			jTextFieldExpiry.setVisible(false);
		}
		return jTextFieldExpiry;
	}


	/**
	 * This method initializes jButtonCheckValid	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCheckValid() {
		if (jButtonCheckValid == null) {
			jButtonCheckValid = new JButton();
			jButtonCheckValid.setBounds(new Rectangle(389, 505, 165, 36));
			jButtonCheckValid.setText("CHECK VALIDATION");
			jButtonCheckValid.setVisible(false);
			jButtonCheckValid.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jLabelErrorMassage1.setVisible(false);
					jLabelErrorMassage2.setVisible(false);
					jLabelErrorMassage3.setVisible(false);
					jLabelExample.setVisible(false);
					jLabelExpiry.setVisible(false);
					jLabelCreditCardNum.setVisible(false);
					jTextFieldCreditNum.setVisible(false);
					jTextFieldExpiry.setVisible(false);	
					jButtonCheckValid.setVisible(false);
					
					Client NewCrdit = new Client(host,DEFAULT_PORT);
					String[] newcredit = new String[4];
					newcredit[0]="new_credit-card";
					newcredit[1]=MainPanel.Userinfo[3];
					newcredit[2]=jTextFieldCreditNum.getText();					
					newcredit[3]=jTextFieldExpiry.getText();
					
					int date = Integer.parseInt(newcredit[3].trim());
					int year = date/100;
					int month = date%100;
						if ((year<=11)||(year>=16)||(month<=00)||(month>12)) 						
						{
							ErrorFrame EFrame = new ErrorFrame();
							EFrame.jLabelMassage.setText("THE DATE IS INCORRECT, PLEASE ENTER AGAIN");
							EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
							EFrame.setSize( 380,217 ); // set frame size
							EFrame.setResizable(true);
							EFrame.setVisible( true ); 	
							jTextFieldExpiry.setText(null);
						}								
						else								
						{
							jLabelValid.setVisible(true);
							NewCrdit.accept(newcredit);
						}
				}
			});
		}
		return jButtonCheckValid;
	}
	
	}
  //  @jve:decl-index=0:visual-constraint="1,15"
